﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace VehicleTrajectoryApp
{   
    public class DYNASMARTVehicle : IVehicle
    {
        int vehicle_id;
        int from_zone_id;
        int to_zone_id;
        float departure_time;
        float arrival_time;
        int complete_flag;
        float trip_time;
        float travel_distance_in_mile;
        int number_of_nodes;
        public int[] linkArray;

        VehicleTrajectoryType type = VehicleTrajectoryType.DYNASMART;

        public int VehicleID
        {
            get
            {
                return vehicle_id;
            }
        }

        public VehicleTrajectoryType TrajectoryType
        {
            get
            {
                return type;
            }
        }

        public int OriginZoneID
        {
            get
            {
                return from_zone_id;
            }
        }
        public int DestinationZoneID
        {
            get
            {
                return to_zone_id;
            }
        }
        public int VehicleStartHour
        {
            get
            {
                return (int)(departure_time / 60);
            }
        }

        public DateTime VehicleStartTime
        {
            get
            {
                return DateTime.Now;
            }
        }

        public float VehicleTravelTime
        {
            get
            {
                return trip_time;
            }
        }

        public List<int> NodeList
        {
            get
            {
                return this.linkArray.ToList();
            }
        }

        public float Travel_Distance_In_Mile
        {
            get
            {
                return travel_distance_in_mile;
            }
        }

        public GPSLink[] GetGPSLinkArray()
        {
            return null;
        }

        public DYNASMARTVehicle(int vehicle_id, int from_zone_id, int to_zone_id,
            float departure_time, float arrival_time, int complete_flag, float trip_time,
            int number_of_nodes)
        {
            this.vehicle_id = vehicle_id;
            this.from_zone_id = from_zone_id;
            this.to_zone_id = to_zone_id;
            this.departure_time = departure_time;
            this.arrival_time = arrival_time;
            this.complete_flag = complete_flag;
            this.trip_time = trip_time;
            this.number_of_nodes = number_of_nodes;

            linkArray = new int[number_of_nodes];
        }

        public string ConvertVehicleTraceToPlacemark(string name, string colorCode)
        {
            string s = "";
            return s;
        }

        public static void AggregatedScenarioStatistics(IEnumerable<IVehicle>[] scenarioVehicleList, out int total_count, out double average, out double std)
        {
            double total = 0;
            double xx = 0;
            total_count = 0;

            if (scenarioVehicleList.Length == 0)
            {
                total_count = 0;
                average = 0;
                std = 0;
            }
            else
            {
                for (int n = 0; n < scenarioVehicleList.Length; n++)
                {
                    foreach (IVehicle v in scenarioVehicleList[n])
                    {
                        total_count++;
                        total += v.VehicleTravelTime;
                        xx += v.VehicleTravelTime * v.VehicleTravelTime;
                    }
                }

                if (total_count > 1)
                {
                    average = total / total_count;
                    std = Math.Sqrt((xx - (total * total) / total_count) / (total_count - 1));
                }
                else
                {
                    if (total_count == 1)
                    {
                        average = total;
                        std = 0;
                    }
                    else
                    {
                        average = 0;
                        std = 0;
                    }
                }
            }
        }

        public static void AggregatedScenarioStatistics(List<IVehicle>[] scenarioVehicleList, out int total_count, out double average, out double std)
        {
            double total = 0;
            double xx = 0;
            total_count = 0;

            if (scenarioVehicleList.Length == 0)
            {
                total_count = 0;
                average = 0;
                std = 0;
            }
            else
            {
                for (int n = 0; n < scenarioVehicleList.Length; n++)
                {
                    foreach (IVehicle v in scenarioVehicleList[n])
                    {
                        total_count++;
                        total += v.VehicleTravelTime;
                        xx += v.VehicleTravelTime * v.VehicleTravelTime;
                    }
                }

                if (total_count > 1)
                {
                    average = total / total_count;
                    std = Math.Sqrt((xx - (total * total) / total_count) / (total_count - 1));
                }
                else
                {
                    if (total_count == 1)
                    {
                        average = total;
                        std = 0;
                    }
                    else
                    {
                        average = 0;
                        std = 0;
                    }
                }
            }
        }

        public static void ScenarioSpecificStatistics(IEnumerable<IVehicle> vehicleList, out int counts, out double average, out double std)
        {
            double total = 0;
            double xx = 0;
            counts = 0;

            foreach (IVehicle v in vehicleList)
            {
                counts++;
                total += v.VehicleTravelTime;
                xx += v.VehicleTravelTime * v.VehicleTravelTime;
            }
            if (counts == 0)
            {
                average = std = 0;
            }
            else
            {
                if (counts == 1)
                {
                    average = total;
                    std = 0;
                }
                else
                {
                    average = total / vehicleList.Count();
                    std = Math.Sqrt((xx - (total * total) / vehicleList.Count()) / (vehicleList.Count() - 1));
                }
            }
        }

        public static void ScenarioSpecificStatistics(List<IVehicle> vehicleList, out int counts, out double average, out double std)
        {
            double total = 0;
            double xx = 0;
            counts = 0;

            foreach (IVehicle v in vehicleList)
            {
                counts++;
                total += v.VehicleTravelTime;
                xx += v.VehicleTravelTime * v.VehicleTravelTime;
            }
            if (counts == 0)
            {
                average = std = 0;
            }
            else
            {
                if (counts == 1)
                {
                    average = total;
                    std = 0;
                }
                else
                {
                    average = total / vehicleList.Count();
                    std = Math.Sqrt((xx - (total * total) / vehicleList.Count()) / (vehicleList.Count() - 1));
                }
            }
        }

        public static List<DYNASMARTVehicle> ReadVehicleFile(string vehicleFileName, int start_time = 6, int end_time = 12)
        {

            if (vehicleFileName.EndsWith("dat"))
            {
                return ReadDYNASMARTVehicleFile(vehicleFileName, start_time, end_time);
            }
            else
            {
                if (vehicleFileName.EndsWith("bin"))
                {
                    return ReadBinaryDYNASMARTVehicleFile(vehicleFileName, start_time, end_time);
                }
                else
                {
                    return ReadVehicleFile(vehicleFileName);
                }
            }
        }

        public void ExtractVehicleLinkFromString(string vehicle_link_string)
        {
            string[] subString = vehicle_link_string.Split('>','<');
            int count = 0;
            for (int n = 0; n < subString.Length; n++)
            {
                if (subString[n].Length == 0)
                {
                    continue;
                }
                else
                {
                    linkArray = new int[number_of_nodes];

                    string[] stringArray = subString[n].Split(';');
                    if (stringArray.Length == 4)
                    {
                        int node_id = Int32.Parse(stringArray[0]);
                        float arrival_time = float.Parse(stringArray[1]);
                        float link_travel_time = float.Parse(stringArray[2]);
                        float emissions = float.Parse(stringArray[3]);
                        
                        if (count < number_of_nodes)
                        {
                            linkArray[count++] = node_id;
                        }
                    }
                }
            }
        }

        public static List<DYNASMARTVehicle> ReadBinaryDYNASMARTVehicleFile(string fileName, int start_time, int end_time)
        {
            List<DYNASMARTVehicle> vehicleList = null;

            BinaryReader file = new BinaryReader(File.Open(fileName, FileMode.Open));
            try
            {
                int vehicle_id;
                int from_zone_id;
                int to_zone_id;
                float departure_time;
                float arrival_time;
                float trip_time;
                float travel_distance_in_mile;
                int number_of_nodes;

                vehicleList = new List<DYNASMARTVehicle>();
                while (true)
                {
                    vehicle_id = file.ReadInt32();
                    from_zone_id = file.ReadInt32();
                    to_zone_id = file.ReadInt32();
                    
                    departure_time = file.ReadSingle();
                    arrival_time = file.ReadSingle();
                    trip_time = file.ReadSingle();
                    travel_distance_in_mile = file.ReadSingle();
                    number_of_nodes = file.ReadInt32();

                    DYNASMARTVehicle vehicle = new DYNASMARTVehicle(vehicle_id, from_zone_id, to_zone_id, departure_time, arrival_time, 0, trip_time,
                        number_of_nodes);
                    vehicle.travel_distance_in_mile = travel_distance_in_mile;
                    vehicle.linkArray = new int[number_of_nodes];

                    int node_no;
                    for (int i = 0; i < number_of_nodes; i++)
                    {
                        node_no = file.ReadInt32();
                        vehicle.linkArray[i] = node_no;
                    }
                    int departure_hour = (int)(departure_time / 60);
                    if (departure_hour >= start_time && departure_hour < end_time)
                    {
                        vehicleList.Add(vehicle);
                    }
                }
            }
            catch (EndOfStreamException)
            {
            }
            catch
            {
                throw;
            }
                
            return vehicleList;
        }

        public static List<DYNASMARTVehicle> ReadDYNASMARTVehicleFile(string fileName, int start_time, int end_time)
        {
            List<DYNASMARTVehicle> vehicleList;

            using (StreamReader file = new StreamReader (fileName))
            {
                int vehicle_id;
                int from_zone_id;
                int to_zone_id;
                float departure_time;
                float arrival_time;
                int complete_flag;
                float trip_time;
                int demand_type;
                int pricing_type;
                //int vehicle_type;
                //int information_type;
                //float value_of_time;
                //float toll_cost_in_dollar;
                //float emissions;
                //float distance_in_mile;
                int number_of_nodes;
                //string path_sequence_string;

                vehicleList = new List<DYNASMARTVehicle>();
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    if (line.IndexOf("Veh") != -1)
                    {
                        //string[] subString = System.Text.RegularExpressions.Regex.Split(line,@"\s+");
                        //string[] subString = System.Text.RegularExpressions.Regex.Split(line, @"[0-9.]+");

                        MatchCollection matches = Regex.Matches(line, @"[0-9.]+");

                        List<float> values = new List<float>();
                        foreach (Match match in matches)
                        {
                            values.Add(float.Parse(match.Groups[0].Value));
                        }

                        if (values.Count == 13)
                        {
                            vehicle_id = (int)values[0];
                            complete_flag = (int)values[1];

                            // if this vehicle doesn't finished its trip
                            if (complete_flag < 2)
                            {
                                continue;
                            }

                            from_zone_id = (int)values[2];
                            to_zone_id = (int)values[3];
                            //information_type = (int)values[4];

                            int upstreamNode = (int)values[5];
                            int downstreamNode = (int)values[6];
                            int destinationNode = (int)values[7];
                            departure_time = values[8];
                            trip_time = values[9];
                            arrival_time = departure_time + trip_time;

                            number_of_nodes = (int)values[10] + 1;
                            demand_type = (int)values[11];
                            pricing_type = (int)values[12];

                            //vehicle_type = 0;
                            //value_of_time = 10.0f;
                            //toll_cost_in_dollar = 0.0f;
                            //emissions = 0.0f;
                            //distance_in_mile = 0.0f;

                            DYNASMARTVehicle vehicle = new DYNASMARTVehicle(vehicle_id, from_zone_id, to_zone_id,
                                departure_time, arrival_time, complete_flag, trip_time, 
                                number_of_nodes);

                            vehicle.linkArray[0] = upstreamNode;

                            
                            int nRead = 1;
                            while (nRead < number_of_nodes)
                            {
                                line = file.ReadLine();
                                string[] strArray = System.Text.RegularExpressions.Regex.Split(line.Trim(), @"\s+");
                                for (int n = 0; n < strArray.Length; n++)
                                {
                                    int value;
                                    if (Int32.TryParse(strArray[n], out value))
                                    {
                                        vehicle.linkArray[nRead] = value;
                                        nRead++;
                                    }
                                }
                            }

                            nRead = 1;
                            while (nRead < number_of_nodes)
                            {
                                line = file.ReadLine();
                                string[] strArray = System.Text.RegularExpressions.Regex.Split(line.Trim(), @"\s+");
                                for (int n = 0; n < strArray.Length; n++)
                                {
                                    float value;
                                    if (float.TryParse(strArray[n], out value))
                                    {
                                        //vehicle.linkArray[nRead].arrivaltime_on_dsn = value + departure_time;
                                        nRead++;
                                    }
                                }
                            }

                            nRead = 1;
                            while (nRead < number_of_nodes)
                            {
                                line = file.ReadLine();
                                string[] strArray = System.Text.RegularExpressions.Regex.Split(line.Trim(), @"\s+");
                                for (int n = 0; n < strArray.Length; n++)
                                {
                                    float value;
                                    if (float.TryParse(strArray[n], out value))
                                    {
                                        //vehicle.linkArray[nRead].link_travel_time = value;
                                        nRead++;
                                    }
                                }
                            }

                            int departure_hour = (int)(departure_time / 60) + 6;
                            if (departure_hour >= start_time && departure_hour < end_time)
                            {
                                vehicleList.Add(vehicle);
                            }

                        }
                    }
                }

                return vehicleList;
            }
        }

        public static void ConvertTextDTAVehicleFileToBinaryVehicleFile(string srcFile, string destFile)
        {
            using (BinaryWriter binFileWriter = new BinaryWriter(File.Open(destFile, FileMode.Create)))
            {
                CSVParser parser = new CSVParser();
                if (parser.OpenCSVFile(srcFile))
                {
                    int vehicle_id;
                    int from_zone_id;
                    int to_zone_id;
                    float departure_time;
                    float arrival_time;
                    int complete_flag;
                    float trip_time;
                    int demand_type;
                    int pricing_type;
                    int vehicle_type;
                    int information_type;
                    float value_of_time;
                    float toll_cost_in_dollar;
                    float emissions;
                    float distance_in_mile;
                    int number_of_nodes;
                    string path_sequence_string;

                    while (parser.ReadRecord())
                    {
                        if (parser.GetValueByFieldName("vehicle_id", out vehicle_id)
                            && parser.GetValueByFieldName("from_zone_id", out from_zone_id)
                            && parser.GetValueByFieldName("to_zone_id", out to_zone_id)
                            && parser.GetValueByFieldName("departure_time", out departure_time)
                            && parser.GetValueByFieldName("arrival_time", out arrival_time)
                            && parser.GetValueByFieldName("complete_flag", out complete_flag)
                            && parser.GetValueByFieldName("trip_time", out trip_time)
                            && parser.GetValueByFieldName("demand_type", out demand_type)
                            && parser.GetValueByFieldName("pricing_type", out pricing_type)
                            && parser.GetValueByFieldName("vehicle_type", out vehicle_type)
                            && parser.GetValueByFieldName("information_type", out information_type)
                            && parser.GetValueByFieldName("value_of_time", out value_of_time)
                            && parser.GetValueByFieldName("toll_cost_in_dollar", out toll_cost_in_dollar)
                            && parser.GetValueByFieldName("emissions", out emissions)
                            && parser.GetValueByFieldName("distance_in_mile", out distance_in_mile)
                            && parser.GetValueByFieldName("number_of_nodes", out number_of_nodes)
                            && parser.GetValueByFieldName("path_sequence", out path_sequence_string)
                            )
                        {
                            DYNASMARTVehicle vehicle = new DYNASMARTVehicle(vehicle_id, from_zone_id, to_zone_id,
                                departure_time, arrival_time, complete_flag, trip_time, 
                                number_of_nodes);
                            vehicle.ExtractVehicleLinkFromString(path_sequence_string);

                            binFileWriter.Write(vehicle.vehicle_id);
                            binFileWriter.Write(vehicle.complete_flag);
                            binFileWriter.Write(vehicle.from_zone_id);
                            binFileWriter.Write(vehicle.to_zone_id);
                            binFileWriter.Write(vehicle.trip_time);
                            //binFileWriter.Write(vehicle.distance_in_mile);
                            binFileWriter.Write(vehicle.departure_time);
                            binFileWriter.Write(vehicle.arrival_time);
                            binFileWriter.Write(vehicle.number_of_nodes);
                            for (int j = 0; j < vehicle.linkArray.Length; j++)
                            {
                                binFileWriter.Write(vehicle.linkArray[j]);
                                //binFileWriter.Write(vehicle.linkArray[j].arrivaltime_on_dsn);
                                //binFileWriter.Write(vehicle.linkArray[j].link_travel_time);
                            }
                        }
                    }
                }
            }
        }

        public static void ConvertTextDYNASMARTVehicleFileToBinaryVehicleFile(string srcFile, string destFile, string networkFile, float simulation_start_min = 360.0f, string mappingFile = null)
        {
            Dictionary<ulong, float> linkLengthMap = null;

            if (File.Exists(networkFile))
            {
                linkLengthMap = new Dictionary<ulong, float>();
                using (StreamReader reader = new StreamReader(networkFile))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] strArray = System.Text.RegularExpressions.Regex.Split(line.Trim(), @"\s+");
                        if (strArray.Length > 10)
                        {
                            int from_node_id;
                            int to_node_id_id;
                            int link_length_in_feet;
                            float link_length_in_miles;

                            from_node_id = Int32.Parse(strArray[0]);
                            to_node_id_id = Int32.Parse(strArray[1]);
                            link_length_in_feet = Int32.Parse(strArray[4]);
                            link_length_in_miles = link_length_in_feet / 5280.0f;

                            ulong key = (((ulong)from_node_id) << 32) | ((uint)(to_node_id_id));
                            linkLengthMap[key] = link_length_in_miles;
                        }
                    }                    
                }
            }

            Dictionary<int, int> NodeIDToBPMZoneIDMap = null;

            if (mappingFile != null && File.Exists(mappingFile))
            {
                NodeIDToBPMZoneIDMap = new Dictionary<int, int>();
                int DYNANodeID;
                int BPMZoneID;

                CSVParser parser = new CSVParser();
                if (parser.OpenCSVFile(mappingFile))
                {
                    while (parser.ReadRecord())
                    {
                        if (parser.GetValueByFieldName("Node_ID", out DYNANodeID) &&
                            parser.GetValueByFieldName("TAZ", out BPMZoneID))
                        {
                            NodeIDToBPMZoneIDMap[DYNANodeID] = BPMZoneID;
                        }
                        
                    }
                }
            }

            int num_of_vehicles_read = 0;
            using (BinaryWriter binFileWriter = new BinaryWriter(File.Open(destFile, FileMode.Create)))
            {
                using (StreamReader file = new StreamReader(srcFile))
                {
                    int vehicle_id;
                    int from_zone_id;
                    int to_zone_id;
                    float departure_time;
                    float arrival_time;
                    int complete_flag;
                    float trip_time;
                    float travel_distance_in_mile;
                    int number_of_nodes;

                    string line;
                    bool bLoad = true;
                    while ((line = file.ReadLine()) != null)
                    {
                        if (line.IndexOf("Veh") != -1)
                        {
                            bLoad = true;
                            MatchCollection matches = Regex.Matches(line, @"[0-9.]+");

                            List<float> values = new List<float>();
                            foreach (Match match in matches)
                            {
                                values.Add(float.Parse(match.Groups[0].Value));
                            }

                            if (values.Count == 13)
                            {
                                vehicle_id = (int)values[0];
                                complete_flag = (int)values[1];

                                // if this vehicle doesn't finished its trip
                                if (complete_flag < 2)
                                {
                                    continue;
                                }

                                from_zone_id = (int)values[2];
                                to_zone_id = (int)values[3];

                                int upstreamNode = (int)values[5];
                                int downstreamNode = (int)values[6];
                                int destinationNode = (int)values[7];

                                if (NodeIDToBPMZoneIDMap != null)
                                {
                                    if (NodeIDToBPMZoneIDMap.ContainsKey(upstreamNode) && NodeIDToBPMZoneIDMap.ContainsKey(destinationNode))
                                    {
                                        from_zone_id = NodeIDToBPMZoneIDMap[upstreamNode];
                                        to_zone_id = NodeIDToBPMZoneIDMap[destinationNode];
                                        bLoad = true;
                                    }
                                    else
                                    {
                                        bLoad = false;
                                        continue;
                                    }
                                }

                                departure_time = values[8] + simulation_start_min;
                                trip_time = values[9];
                                arrival_time = departure_time + trip_time;

                                number_of_nodes = (int)values[10] + 1;

                                DYNASMARTVehicle vehicle = new DYNASMARTVehicle(vehicle_id, from_zone_id, to_zone_id,
                                    departure_time, arrival_time, complete_flag, trip_time, 
                                    number_of_nodes);

                                vehicle.linkArray[0] = upstreamNode;


                                int nRead = 1;
                                while (nRead < number_of_nodes)
                                {
                                    line = file.ReadLine();
                                    string[] strArray = System.Text.RegularExpressions.Regex.Split(line.Trim(), @"\s+");
                                    for (int n = 0; n < strArray.Length; n++)
                                    {
                                        int value;
                                        if (Int32.TryParse(strArray[n], out value))
                                        {
                                            vehicle.linkArray[nRead] = value;
                                            nRead++;
                                        }
                                    }
                                }

                                nRead = 1;
                                while (nRead < number_of_nodes)
                                {
                                    line = file.ReadLine();
                                    string[] strArray = System.Text.RegularExpressions.Regex.Split(line.Trim(), @"\s+");
                                    for (int n = 0; n < strArray.Length; n++)
                                    {
                                        float value;
                                        if (float.TryParse(strArray[n], out value))
                                        {
                                            //vehicle.linkArray[nRead].arrivaltime_on_dsn = value + departure_time;
                                            nRead++;
                                        }
                                    }
                                }

                                nRead = 1;
                                while (nRead < number_of_nodes)
                                {
                                    line = file.ReadLine();
                                    string[] strArray = System.Text.RegularExpressions.Regex.Split(line.Trim(), @"\s+");
                                    for (int n = 0; n < strArray.Length; n++)
                                    {
                                        float value;
                                        if (float.TryParse(strArray[n], out value))
                                        {
                                            //vehicle.linkArray[nRead].link_travel_time = value;
                                            nRead++;
                                        }
                                    }
                                }

                                travel_distance_in_mile = 0.0f;
                                for (int j = 0; j < vehicle.linkArray.Length - 1; j++)
                                {
                                    ulong key = (ulong)(vehicle.linkArray[j]) << 32 | (uint)(vehicle.linkArray[j + 1]);
                                    if (linkLengthMap.ContainsKey(key))
                                    {
                                        travel_distance_in_mile += linkLengthMap[key];
                                    }
                                }
                                vehicle.travel_distance_in_mile = travel_distance_in_mile;

                                if (bLoad)
                                {
                                    binFileWriter.Write(vehicle.vehicle_id);
                                    
                                    //binFileWriter.Write(vehicle.complete_flag);
                                    
                                    binFileWriter.Write(vehicle.from_zone_id);
                                    binFileWriter.Write(vehicle.to_zone_id);
                                    
                                    //binFileWriter.Write(vehicle.distance_in_mile);

                                    binFileWriter.Write(vehicle.departure_time);
                                    binFileWriter.Write(vehicle.arrival_time);
                                    binFileWriter.Write(vehicle.trip_time);
                                    binFileWriter.Write(vehicle.travel_distance_in_mile);
                                    binFileWriter.Write(vehicle.number_of_nodes);                                    
                                    for (int j = 0; j < vehicle.linkArray.Length; j++)
                                    {
                                        binFileWriter.Write(vehicle.linkArray[j]);
                                        //binFileWriter.Write(vehicle.linkArray[j].arrivaltime_on_dsn);
                                        //binFileWriter.Write(vehicle.linkArray[j].link_travel_time);
                                    }

                                    num_of_vehicles_read++;
                                }
                            }
                        }
                    }
                }
            }

            string logFileName = System.IO.Path.GetDirectoryName(destFile) + @"\ConvertingLog.txt";
            using (StreamWriter logWriter = new StreamWriter(logFileName))
            {
                logWriter.WriteLine("{0} vehilces are read!", num_of_vehicles_read);
            }
        }

        public static List<DYNASMARTVehicle> ReadVehicleInfoFile(string fileName)
        {
            List<DYNASMARTVehicle> vehicleList;

            CSVParser parser = new CSVParser();
            if (parser.OpenCSVFile(fileName))
            {
                int vehicle_id;
                int from_zone_id;
                int to_zone_id;
                float departure_time;
                float arrival_time;
                int complete_flag;
                float trip_time;
                int demand_type;
                int pricing_type;
                int vehicle_type;
                int information_type;
                float value_of_time;
                float toll_cost_in_dollar;
                float emissions;
                float distance_in_mile;
                int number_of_nodes;
                string path_sequence_string;

                vehicleList = new List<DYNASMARTVehicle>();

                while (parser.ReadRecord())
                {
                    if (parser.GetValueByFieldName("vehicle_id", out vehicle_id)
                        && parser.GetValueByFieldName("from_zone_id", out from_zone_id)
                        && parser.GetValueByFieldName("to_zone_id", out to_zone_id)
                        && parser.GetValueByFieldName("departure_time", out departure_time)
                        && parser.GetValueByFieldName("arrival_time", out arrival_time)
                        && parser.GetValueByFieldName("complete_flag", out complete_flag)
                        && parser.GetValueByFieldName("trip_time", out trip_time)
                        && parser.GetValueByFieldName("demand_type", out demand_type)
                        && parser.GetValueByFieldName("pricing_type", out pricing_type)
                        && parser.GetValueByFieldName("vehicle_type", out vehicle_type)
                        && parser.GetValueByFieldName("information_type", out information_type)
                        && parser.GetValueByFieldName("value_of_time", out value_of_time)
                        && parser.GetValueByFieldName("toll_cost_in_dollar", out toll_cost_in_dollar)
                        && parser.GetValueByFieldName("emissions", out emissions)
                        && parser.GetValueByFieldName("distance_in_mile", out distance_in_mile)
                        && parser.GetValueByFieldName("number_of_nodes", out number_of_nodes)
                        && parser.GetValueByFieldName("path_sequence", out path_sequence_string)
                        )
                    {
                        DYNASMARTVehicle vehicle = new DYNASMARTVehicle(vehicle_id, from_zone_id, to_zone_id,
                            departure_time, arrival_time, complete_flag, trip_time,
                            number_of_nodes);
                        vehicle.ExtractVehicleLinkFromString(path_sequence_string);
                        vehicleList.Add(vehicle);
                    }

                }
                return vehicleList;
            }
            else
            {
                return null;
            }
        }
    }
}
